<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input
              v-model="serchMoudle.roleName"
              clearable
              placeholder="请输入角色名称"
              @clear="clear('user')"
            /></el-col>
          <el-col :span="8">
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="getList"
            >搜索</el-button>
            <el-button
              type="info"
              icon="el-icon-refresh"
              @click="resetList"
            >重置</el-button>
            <el-button
              type="primary"
              icon="el-icon-circle-plus"
              @click="saveRoleUI(null)"
            >新增</el-button>
          </el-col>
        </el-row>
      </div>
      <div class="text item">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="date" label="#" width="80">
            <template slot-scope="scope">
              {{
                (serchMoudle.pageNo - 1) * serchMoudle.pageSize +
                  scope.$index +
                  1
              }}
            </template>
          </el-table-column>
          <el-table-column prop="roleId" label="角色id" width="80" />
          <el-table-column prop="roleName" label="角色名称" width="180" />
          <el-table-column prop="roleDesc" label="角色描述" />
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                circle
                @click="saveRoleUI(scope.row.roleId)"
              />
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                circle
                @click="delRole(scope.row.roleId)"
              />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <el-pagination
      :current-page="currentPage4"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="serchMoudle.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 新增/编辑弹窗 -->

    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      @close="clearroleForm"
    >
      <el-form ref="roleForm" :rules="rules" :model="roleForm">
        <el-form-item
          label="角色名称"
          :label-width="formLabelWidth"
          prop="roleName"
        >
          <el-input v-model="roleForm.roleName" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="角色描述"
          :label-width="formLabelWidth"
          prop="roleDesc"
        >
          <el-input v-model="roleForm.roleDesc" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="角色权限"
          :label-width="formLabelWidth"
          prop="menuIdList"
        >
          <el-tree
            ref="menuRef"
            :data="menuList"
            style="width:80%"
            show-checkbox
            :props="menuProps"
            default-expand-all
            highlight-current
            node-key="menuId"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import roleApi from '@/api/roleManage'
import menuApi from '@/api/menuManage'
export default {
  name: 'RoleList',
  components: {},
  data() {
    return {
      menuList: [],
      menuProps: {
        children: 'children',
        label: 'title'
      },
      roleForm: {},
      title: '',
      dialogFormVisible: false,
      formLabelWidth: '120px',
      input: '',
      input1: '',
      total: 0,
      serchMoudle: {
        roleName: '',
        pageNo: 1,
        pageSize: 10
      },
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          {
            min: 3,
            max: 16,
            message: '长度在 3 到 16 个字符',
            trigger: 'blur'
          }
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          {
            min: 3,
            max: 16,
            message: '长度在 3 到 16 个字符',
            trigger: 'blur'
          }
        ]
      },
      currentPage4: 1,
      tableData: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList()
    this.getAllMenuList()
  },
  methods: {
    getAllMenuList() {
      menuApi.getAllMenu().then(res => {
        console.log(res)
        if (res.code === 20000) {
          this.menuList = res.data
        }
      })
    },
    delRole(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        roleApi.deleteRole(id).then((res) => {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.getList()
        })
      })
    },
    saveRole() {
      this.$refs.roleForm.validate((valid) => {
        if (valid) {
          const menuCheckedId = this.$refs.menuRef.getCheckedKeys()
          const menuHalfCheckedId = this.$refs.menuRef.getHalfCheckedKeys()
          console.log(menuCheckedId, menuHalfCheckedId)
          this.roleForm.menuIdList = menuCheckedId.concat(menuHalfCheckedId)
          roleApi.saveRole(this.roleForm).then((res) => {
            this.$message({
              message: res.message,
              type: 'success'
            })
            this.dialogFormVisible = false
            this.getList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clearroleForm() {
      this.roleForm = {}
      this.$refs.roleForm.resetFields()
      this.$refs.menuRef.setCheckedKeys([])
    },
    saveRoleUI(id) {
      if (id == null || id === undefined) {
        this.title = '新增用户角色'
      } else {
        this.title = '编辑用户角色'
        roleApi.getRoleInfo(id).then((res) => {
          this.$refs.menuRef.setCheckedKeys(res.data.menuIdList)
          this.roleForm = res.data
        })
      }
      this.dialogFormVisible = true
    },
    clear(params) {
      console.log(params)
      if (params === 'phone') {
        this.serchMoudle.phone = ''
        this.getList()
      }
      if (params === 'user') {
        this.serchMoudle.user = ''
        this.getList()
      }
    },
    getList() {
      roleApi.getRoleList(this.serchMoudle).then((res) => {
        console.log(res)
        this.total = res.data.total
        this.tableData = res.data.rows
      })
    },
    resetList() {
      this.serchMoudle.roleName = ''
      this.serchMoudle.phone = ''
      this.serchMoudle.pageNo = 1
      this.serchMoudle.pageSize = 10
      this.getList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.serchMoudle.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.serchMoudle.pageNo = val

      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  margin-bottom: 10px;
}

.el-dialog .el-input {
  width: 80%;
}
</style>
